package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Answer;
import cn.wolfcode.luowowo.article.query.AnswerQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.cache.service.IQuestionRedisService;
import cn.wolfcode.luowowo.cache.vo.AnswerRedisStatisSortVO;
import cn.wolfcode.luowowo.common.util.AJaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 名字
 * @company 公司
 * @create 2019-09-26 15:00
 */
@Controller
@RequestMapping("answer")
public class AnswerController {

    @Reference
    private IAnswerService answerService;


    @Reference
    private IQuestionRedisService questionRedisService;

    @Reference
    private IAnswerRedisService answerRedisService;

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AJaxResult saveOrUpdate(Answer answer, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AJaxResult aJaxResult = new AJaxResult();
            aJaxResult.setCode(102);
            aJaxResult.setSuccess(false);
            return aJaxResult.addData("请登录");
        }

        //添加问答的回答数
        questionRedisService.replyNumIncrease(answer.getQuestionId(), 1);

        //1.设置答复人
        answer.setAuthor(userInfo);
        //2.添加
        answerService.saveOrUpdate(answer);
//
//        //添加排行榜中的回答数
        answerRedisService.addScore(AnswerQuery.CONDITION_ANSWER,answer.getAuthor().getId(),1);


        return new AJaxResult();
    }

    @RequestMapping("rank")
    public String rank(AnswerQuery answerQuery, Model model) {
//        List<AnswerSatisSortVo> vos = answerService.querySort(answerQuery);
        // TODO 待完善
        List<AnswerRedisStatisSortVO> vos = answerRedisService.querySort(answerQuery);
        model.addAttribute("vos", vos);
        return "wenda/rankTpl";
    }

    @RequestMapping("thump")
    @ResponseBody
    public AJaxResult thump(Long aid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AJaxResult aJaxResult = new AJaxResult(false, "");
            aJaxResult.setCode(102);
            return aJaxResult;
        }
        //判断今天是否有点过
        boolean ret = answerRedisService.answerThump(aid, userInfo.getId());
        AJaxResult aJaxResult = new AJaxResult();
        //点过了
        if (ret) {
            answerRedisService.thumpNumIncrease(aid, 1);
            //排行榜分数增加 TODO 性能问题
            Answer answer = answerService.get(aid);
            answerRedisService.addScore(AnswerQuery.CONDITION_THUMB,answer.getAuthor().getId(),1);
        } else {
            aJaxResult.setSuccess(false);
            aJaxResult.setCode(103);
        }
        return aJaxResult.addData(answerRedisService.getStatisVo(aid).getThumbsupnum());
    }
}
